iT邦幫忙

2021 iThome 鐵人賽

DAY 21
0
Software Development

iOS 學習筆記系列 第 23

Day23 URLSession 03 - PUT / PATCH

  • 分享至 

  • xImage
  •  

PUT / PATCH:修改資料

一樣的模式再來一次,根據以上的Reqres API 來示範

首先一樣根據Response 建立Model

User.swift :

struct UserUpdateResponse: Decodable {
    var name: String
    var job: String
    var updatedAt: Date?
    
    var modifiedUpdateAt: String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateStyle = .medium
        if let newDate = updatedAt {
            return dateFormatter.string(from: newDate)
        } else {
            return "******"
        }
    }
    
    enum CodingKeys: String, CodingKey {
        case name
        case job
        case updatedAt
    }
}

接著在UserURLSession.swift處理關於URLSession的事件

    func userUpdateRequest(userRequestBody:UserRequestBody,completionHandler: @escaping (UserUpdateResponse) -> Void) {
        
        let url = URL(string: "https://reqres.in/api/users/2")!
        
        // MARK: URLRequest
        var request = URLRequest(url: url)
        request.httpMethod = "PUT"
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        
        //MARK: POST Request V.1.0
        guard let httpBody = try? JSONEncoder().encode(userRequestBody) else {
            print("Invalid httpBody")
            return
        }
        
        // MARK: Set httpBody
        request.httpBody = httpBody
        
        URLSession.shared.dataTask(with: request) {
            data, response, error in
            if let data = data {
                do {
                    let decoder = JSONDecoder()
                    
                    decoder.dateDecodingStrategy = .formatted(DateFormatter.customFormatter)
                    
                    let user = try decoder.decode(UserUpdateResponse.self, from: data)
                    
                    completionHandler(user)
                    
                }catch(let error) {
                    print(error.localizedDescription)
                }
            } else {
                print("No Data")
            }
        }.resume()
    }
    
}

之後在View 設計畫面並使用
UPDATESampleView.swift

import SwiftUI

struct UPDATESampleView: View {
    @State private var name = "Ryder"
    @State private var job = "Engineer"
    
    @State var user :UserUpdateResponse = UserUpdateResponse(name: "*****", job: "*****", updatedAt: nil)
    
    var body: some View {
        NavigationView {
            VStack {
                List(){
                    Text("UPDATE sample")
                    Section(header: Text("Request Data")) {
                        TextField("Name: ", text: $name)
                        TextField("Job: ", text: $job)
                    }
                }
                List() {
                    Section(header: Text("Response Data")) {
                        Text("Name: \(user.name)")
                        Text("Job: \(user.job)")
                        Text("Date updated: \(user.modifiedUpdateAt)")
                    }
                }
                .listStyle(GroupedListStyle())
                Button {
                    UserURLSession.shared.userUpdateRequest(userRequestBody: UserRequestBody(name: name, job: job)) { newUser in
                        DispatchQueue.main.async {
                            user = newUser
                        }
                    }
                    
                } label: {
                    Text("UPDATE Request")
                }
                .padding()
                .foregroundColor(Color.white)
                .background(Color.green)
                .cornerRadius(8)
                
            }
            .navigationBarTitleDisplayMode(.inline)
            .navigationTitle(Text("URLSession UPDATE Sample"))
        }
        
    }
}

struct UPDATESampleView_Previews: PreviewProvider {
    static var previews: some View {
        UPDATESampleView()
    }
}

這邊所有URLSession 的範例一起放在Github: URLSessionSample 供大家參考


上一篇
Day22 URLSession 02 - GET
下一篇
Day24 URLSession 04 - DELETE
系列文
iOS 學習筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言